package cn.android666.badmintonchargecalculator

import android.content.Context
import android.preference.PreferenceManager

/**
 * SharedPreferences工具类，用于把数据保存到默认配置文件中。
 * 保存的key使用Aes加密，如果是保存String，则value也Aes加密。
 * 保存位置如：/data/data/cn.android666.badmintonchargecalculator/shared_prefs/cn.android666.badmintonchargecalculator_prefrences.xml
 */
class SPUtil(context: Context) {

    private val pref = PreferenceManager.getDefaultSharedPreferences(context)
    private val editor = pref.edit()

    fun putInt(key: String, value: Int) = editor.putInt(key, value).apply()
    fun getInt(key: String, defaultValue: Int)= pref.getInt(key, defaultValue)
    fun putLong(key: String, value: Long) = editor.putLong(key, value).apply()
    fun getLong(key: String, defaultValue: Long) = pref.getLong(key, defaultValue)
    fun putBoolean(key: String, value: Boolean) = editor.putBoolean(key, value).apply()
    fun getBoolean(key: String, defaultValue: Boolean) = pref.getBoolean(key, defaultValue)
    fun putString(key: String, value: String) = editor.putString(key, value).apply()
    fun getString(key: String): String = pref.getString(key, "") ?: ""

}